﻿using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

namespace OpenFlashChartLib.Charts
{
    public class ValuesCollection<T> : IList<ILabelValue>  where T : ILabelValue
    {
        readonly IList<T> collection;

        public ValuesCollection(IList<T> collection)
        {
            this.collection = collection;
        }

        public IEnumerator<ILabelValue> GetEnumerator()
        {
            return new LabelValueEnumerator<T>(collection.GetEnumerator());
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public void Add(ILabelValue item)
        {
            collection.Add((T)item);
        }

        public void Clear()
        {
            collection.Clear();
        }

        public bool Contains(ILabelValue item)
        {
            return collection.Contains((T) item);
        }

        public void CopyTo(ILabelValue[] array, int arrayIndex)
        {
            throw new System.NotImplementedException();
        }

        public bool Remove(ILabelValue item)
        {
            bool res = collection.Remove((T) item);
            return res;
        }

        public int Count
        {
            get { return collection.Count; }
        }

        public bool IsReadOnly
        {
            get { return collection.IsReadOnly; }
        }

        public int IndexOf(ILabelValue item)
        {
            return collection.IndexOf((T) item);
        }

        public void Insert(int index, ILabelValue item)
        {
            collection.Insert(index, (T)item);
            
        }

        public void RemoveAt(int index)
        {
            collection.RemoveAt(index);
        }

        public ILabelValue this[int index]
        {
            get { return collection[index]; }
            set { collection[index] = (T)value; }
        }
    }

    public class LabelValueEnumerator<T>: IEnumerator<ILabelValue>
    {
        private readonly IEnumerator<T> enumerator;

        public LabelValueEnumerator(IEnumerator<T> enumerator)
        {
            this.enumerator = enumerator;
        }

        public void Dispose()
        {
            enumerator.Dispose();
        }

        public bool MoveNext()
        {
            return enumerator.MoveNext();
        }

        public void Reset()
        {
            enumerator.Reset();
        }

        public ILabelValue Current
        {
            get { return (ILabelValue)enumerator.Current; }
        }

        object IEnumerator.Current
        {
            get { return Current; }
        }
    }

    public delegate void CollectionModifiedDeleg<T> (object sender, T value);
}
